package com.dcivision.audit.core;
/*
 * @(#)AuditTrailManager.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.audit.AuditTrailConstant;
import com.dcivision.audit.bean.AuditTrail;
import com.dcivision.audit.dao.AuditTrailDAObject;
import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  AuditTrailManager.java

  This class manage the general operation for audit trail.

    @author           Zoe Shum
    @company          DCIVision Limited
    @creation date    11/09/2003
    @version          $Revision: 1.17.2.1 $
*/

public class AuditTrailManager {

  public static final String REVISION = "$Revision: 1.17.2.1 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());
  SessionContainer sessionContainer = null;
  Connection conn = null;

  public AuditTrailManager(SessionContainer sessionCon, Connection connection) {
    this.sessionContainer = sessionCon;
    this.conn = connection;
  }

  public void release() {
    this.sessionContainer = null;
    this.conn = null;
  }

  public void auditTrail(String objectType, AbstractBaseObject object, String accessType) throws ApplicationException {
      auditTrail(objectType, object, accessType, null);
  }

  public void auditTrail(String objectType, AbstractBaseObject object, String accessType, String version) throws ApplicationException {
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);

    String userIP = sessionContainer.getUserIPAddress();
    AuditTrail audit = new AuditTrail();
    log.debug("Object Type: " + objectType + " Object Id: "+ object.getID() + " Access Type: "+accessType + " Version: "+version);
    audit.setObjectType(objectType);
    audit.setObjectID(object.getID());
    audit.setVersionNumber(version);
    audit.setAccessType(accessType);
    audit.setSessionID(sessionContainer.getSessionID());
    audit.setAccessorID(sessionContainer.getUserRecordID());
    audit.setIpAddress(userIP);

    AuditTrailDAObject auditDAO  = new AuditTrailDAObject(sessionContainer, conn);
    AuditTrail returnObj;
    //version only exist for Document by not for Folder
    if (GlobalConstant.OBJECT_TYPE_DOCUMENT.equals(objectType)) {
      if (version==null || (DmsOperationConstant.DMS_TOP_VERSION.equals(version))) {

        DmsVersion dmsVersion = new DmsVersion();
        //if (DmsOperationConstant.DMS_TOP_VERSION.equals(version)){
          dmsVersion =docRetrievalManager.getTopVersionByDocumentID(object.getID());
          version = dmsVersion.getVersionNumber();
        //}
      }
      audit.setVersionNumber(version);
      DmsDocument dmsDoc = (DmsDocument)object;

      audit.setObjectName(dmsDoc.getDocumentName());
      
      String sCurFunctionCode = dmsDoc.getCurFunctionCode();
      //if (Utility.isEmpty(sCurFunctionCode)){
        sCurFunctionCode = docRetrievalManager.getCurFunctionCode(dmsDoc);
      //}
      log.debug(" VERSION = " + version + " sCurFunctionCode = " + sCurFunctionCode);
      if (SystemFunctionConstant.DMS_PUBLIC_FOLDER.equals(sCurFunctionCode) || AuditTrailConstant.ACCESS_TYPE_VIEW.equals(accessType)){
        returnObj = (AuditTrail) auditDAO.insertObject(audit);
      }
    }
    docRetrievalManager.release();
    auditDAO=null;
  }


  public synchronized List getDmsAccessList(String accessList, AbstractSearchForm form) throws ApplicationException {
    AuditTrailDAObject auditTrailDAO = new AuditTrailDAObject(sessionContainer, conn);
    List resultList = new ArrayList();
    synchronized (conn) {
      resultList = auditTrailDAO.getDmsAccessList(accessList, form);
    }
    auditTrailDAO = null;
    return resultList;
  }

  public void deleteListByObjectTypeObjectID(String objectType, Integer objectID) throws ApplicationException {
    AuditTrailDAObject auditTrailDAO = new AuditTrailDAObject(sessionContainer, conn);
    auditTrailDAO.deleteListByObjectTypeObjectID(objectType, objectID);
  }
}